home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter6 / parmopts.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  14KB  |  376 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "ParmOpts.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLParamOptions()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. // buffer lengths
  109. // ..............
  110. #define NAME_LEN 20
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HENV  hEnv  = NULL;
  115. static HDBC  hDBC  = NULL;
  116. static HWND  hList = NULL;
  117. static PTR   pParamData;
  118.  
  119. static UCHAR szSqlStr[] = 
  120.    "Insert into department( dept_id, dept_name, dept_head_id ) "
  121.    "Values ( ?, ?, ? )";
  122. static UCHAR szLBStr[128];
  123.  
  124. // data being sent to the employee table.
  125. // ......................................
  126.  
  127. static int   nDeptId    [3]             = { 600,          700,          800          };
  128. static UCHAR sDeptName  [3][NAME_LEN+1] = { "Accounting", "Accounting", "Recruiting" };
  129. static int   nDeptHeadId[3]             = { 501,          703,          703          };
  130.  
  131. static SDWORD cbIdLen    [3];
  132. static SDWORD cbNameLen  [3];
  133. static SDWORD cbHeadIdLen[3];
  134.  
  135.    switch( uMsg )
  136.    {
  137.       case WM_CREATE :
  138.               {
  139.                  RETCODE retCode;
  140.  
  141.                  // Allocate Environment and Connection.
  142.                  //.....................................
  143.                  SQLAllocEnv( &hEnv );
  144.                  SQLAllocConnect( hEnv, &hDBC );
  145.  
  146.                  // Connect to the data source
  147.                  //...........................
  148.                  retCode = SQLConnect( hDBC, 
  149.                              "Test Data Source", SQL_NTS,
  150.                              "DBA",              SQL_NTS,  
  151.                              "SQL",              SQL_NTS );
  152.  
  153.                  if ( retCode != SQL_SUCCESS )
  154.                  {
  155.                     SQLFreeConnect( hDBC );
  156.                     SQLFreeEnv( hEnv );
  157.                     return( -1 );
  158.                  }
  159.  
  160.                  if ( retCode == SQL_SUCCESS )
  161.                  {
  162.                     hList = CreateWindow( "LISTBOX", "",    
  163.                                           LBS_NOTIFY | WS_VSCROLL | 
  164.                                           WS_BORDER  | WS_CHILD | 
  165.                                           WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  166.                                           0, 0, 
  167.                                           0, 0,  
  168.                                           hWnd,              
  169.                                           (HMENU)101,              
  170.                                           hInst,         
  171.                                           NULL               
  172.                                         );
  173.                  }
  174.               }
  175.               break;
  176.  
  177.       case WM_SIZE :
  178.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  179.                                        HIWORD( lParam ), TRUE );
  180.               break; 
  181.               
  182.       case WM_COMMAND :
  183.               switch( LOWORD( wParam ) )
  184.               {
  185.                  case IDM_TEST :
  186.                         {
  187.                            HSTMT   hStmt;
  188.                            RETCODE retCode;
  189.                            int     i = 0;
  190.                            UDWORD  iRow = 0L;
  191.  
  192.                            // Allocate a statement handle for the query.
  193.                            // ..........................................
  194.                            SQLAllocStmt( hDBC, &hStmt );
  195.  
  196.                            // Call SQLPrepare() to compile the query.
  197.                            // .......................................
  198.                            retCode = SQLPrepare( hStmt, szSqlStr, SQL_NTS );
  199.                            
  200.                            if( retCode != SQL_SUCCESS && 
  201.                                retCode != SQL_SUCCESS_WITH_INFO )
  202.                            {
  203.                               SQLFreeStmt( hStmt, SQL_DROP );
  204.                               break;
  205.                            }
  206.                            
  207.                            // Call SQLBindParameter() to 
  208.                            // bind buffers for the columns.
  209.                            // .............................
  210.                            SQLBindParameter( hStmt, 1, SQL_PARAM_INPUT, 
  211.                                              SQL_C_DEFAULT, SQL_INTEGER,
  212.                                              0, 0, 
  213.                                              nDeptId, 0,
  214.                                              cbIdLen );
  215.                            
  216.                            SQLBindParameter( hStmt, 2, SQL_PARAM_INPUT, 
  217.                                              SQL_C_CHAR, SQL_CHAR, 
  218.                                              NAME_LEN, 0, 
  219.                                              sDeptName, sizeof(sDeptName),  
  220.                                              cbNameLen );
  221.  
  222.                            SQLBindParameter( hStmt, 3, SQL_PARAM_INPUT, 
  223.                                              SQL_C_DEFAULT, SQL_INTEGER, 
  224.                                              0, 0, 
  225.                                              nDeptHeadId, 0,
  226.                                              cbHeadIdLen );
  227.                            
  228.                            
  229.                            // Use SQLParamOptions() to specify passage of 
  230.                            // three parameters to the statement handle.
  231.                            // ...........................................
  232.                            SQLParamOptions( hStmt, 3, &iRow );
  233.  
  234.                            // Call SQLExecute() to execute the query.
  235.                            // .......................................
  236.                            retCode = SQLExecute( hStmt );
  237.  
  238.                            // Error handling.
  239.                            // ...............
  240.                            while( retCode != SQL_SUCCESS && 
  241.                                   retCode != SQL_SUCCESS_WITH_INFO )
  242.                            {
  243.                               char szLBStr[128];
  244.  
  245.                               // If iRow remains unchanged, the query 
  246.                               // failed, so rollback the transaction and
  247.                               // quit processing.
  248.                               //
  249.                               // If iRow == 1, the  first row failed, 
  250.                               // rollback the transaction and terminate
  251.                               // processing.
  252.                               // .......................................
  253.                               if( iRow == 0 || iRow == 1 )
  254.                               {
  255.                                  // Rollback the transaction.
  256.                                  // .........................
  257.                                  SQLTransact( SQL_NULL_HENV, hDBC,
  258.                                               SQL_ROLLBACK);
  259.  
  260.                                  SendMessage( hList, LB_ADDSTRING, 
  261.                                     0, (LPARAM)"Inserts failed." );
  262.  
  263.                                  break;
  264.                               }
  265.  
  266.                               // Attempt to process the remaining records.
  267.                               // .........................................
  268.                               sprintf( szLBStr, "Insert of row %ld failed.",
  269.                                                  iRow );
  270.  
  271.                               SendMessage( hList, LB_ADDSTRING, 
  272.                                            0, (LPARAM)szLBStr );
  273.  
  274.                               // Rebind parameters.
  275.                               // ..................
  276.  
  277.                               SQLBindParameter( hStmt, 1, SQL_PARAM_INPUT, 
  278.                                                 SQL_C_DEFAULT, SQL_INTEGER,
  279.                                                 0, 0, 
  280.                                                 &nDeptId[iRow], 0,
  281.                                                 &cbIdLen[iRow] );
  282.                            
  283.                               SQLBindParameter( hStmt, 2, SQL_PARAM_INPUT, 
  284.                                                 SQL_C_CHAR, SQL_CHAR, 
  285.                                                 NAME_LEN, 0, 
  286.                                                 &sDeptName[iRow], sizeof(sDeptName),  
  287.                                                 &cbNameLen[iRow] );
  288.  
  289.                               SQLBindParameter( hStmt, 3, SQL_PARAM_INPUT, 
  290.                                                 SQL_C_DEFAULT, SQL_INTEGER, 
  291.                                                 0, 0, 
  292.                                                 &nDeptHeadId[iRow], 0,
  293.                                                 &cbHeadIdLen[iRow] );
  294.  
  295.  
  296.                               // Reset parameter options
  297.                               // with SQLParamOptions().
  298.                               // .......................
  299.                               SQLParamOptions( hStmt, 3-iRow, &iRow );
  300.  
  301.                               // Call SQLExecute() to insert 
  302.                               // the remaining rows.
  303.                               // ...........................
  304.                               retCode = SQLExecute( hStmt );
  305.                            }
  306.  
  307.                            if( retCode == SQL_SUCCESS || 
  308.                                retCode == SQL_SUCCESS_WITH_INFO )
  309.                            {
  310.                               // Commit the transaction.
  311.                               // .......................
  312.                               SQLTransact( SQL_NULL_HENV, hDBC, SQL_COMMIT );
  313.  
  314.                               SendMessage( hList, LB_ADDSTRING, 
  315.                                            0, (LPARAM)"Inserts successful." );
  316.                            }
  317.                            
  318.                            // Free the statement handle.
  319.                            // ..........................
  320.                            SQLFreeStmt( hStmt, SQL_DROP );
  321.                         }
  322.                         break;
  323.  
  324.                  case IDM_ABOUT :
  325.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  326.                         break;
  327.  
  328.                  case IDM_EXIT :
  329.                         DestroyWindow( hWnd );
  330.                         break;
  331.               }
  332.               break;
  333.       
  334.       case WM_DESTROY :
  335.               PostQuitMessage(0);
  336.  
  337.               // Disconnect Environment.
  338.               //........................
  339.               SQLDisconnect( hDBC );
  340.  
  341.               // Free Connection and Environment.
  342.               //.................................
  343.               SQLFreeConnect( hDBC );
  344.               SQLFreeEnv( hEnv );
  345.               break;
  346.  
  347.       default :
  348.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  349.    }
  350.  
  351.    return( 0L );
  352. }
  353.  
  354. LRESULT CALLBACK About( HWND hDlg,           
  355.                         UINT message,        
  356.                         WPARAM wParam,       
  357.                         LPARAM lParam)
  358. {
  359.    switch (message) 
  360.    {
  361.        case WM_INITDIALOG: 
  362.                return (TRUE);
  363.  
  364.        case WM_COMMAND:                              
  365.                if (   LOWORD(wParam) == IDOK         
  366.                    || LOWORD(wParam) == IDCANCEL)    
  367.                {
  368.                        EndDialog(hDlg, TRUE);        
  369.                        return (TRUE);
  370.                }
  371.                break;
  372.    }
  373.  
  374.    return (FALSE); 
  375. }
  376.